package com.pinganfu.tesla.iwallet;

import java.util.HashMap;
import java.util.Map;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

import com.pinganfu.tesla.iwallet.tab.TabExploreActivity;
import com.pinganfu.tesla.iwallet.tab.TabFortuneActivity;
import com.pinganfu.tesla.iwallet.tab.TabHomeActivity;
import com.pinganfu.tesla.iwallet.tab.TabServiceActivity;
import com.pinganfu.tesla.iwallet.util.LogUtil;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnTabChangeListener {

	private TabHost mTabHost = null;
	private Map<String, Map<String, Object>> mTabData = new HashMap<String, Map<String, Object>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mTabHost = getTabHost();

		initTabs();

		mTabHost.setOnTabChangedListener(this);
	}

	private void initTabs() {
		String[] tabIds = new String[]{"0", "1", "2", "3"};

		Map<String, Object> m1 = new HashMap<String, Object>();
		m1.put("name_res_id", R.string.lbl_home);
		m1.put("res_id", R.drawable.ic_ft_tesla);
		m1.put("res_id_selected", R.drawable.ic_ft_tesla_selected);
		m1.put("intent", TabHomeActivity.class);
		mTabData.put(tabIds[0], m1);

		Map<String, Object> m2 = new HashMap<String, Object>();
		m2.put("name_res_id", R.string.lbl_service);
		m2.put("res_id", R.drawable.ic_ft_service);
		m2.put("res_id_selected", R.drawable.ic_ft_service_selected);
		m2.put("intent", TabServiceActivity.class);
		mTabData.put(tabIds[1], m2);

		Map<String, Object> m3 = new HashMap<String, Object>();
		m3.put("name_res_id", R.string.lbl_explore);
		m3.put("res_id", R.drawable.ic_ft_explore);
		m3.put("res_id_selected", R.drawable.ic_ft_explore_selected);
		m3.put("intent", TabExploreActivity.class);
		mTabData.put(tabIds[2], m3);

		Map<String, Object> m4 = new HashMap<String, Object>();
		m4.put("name_res_id", R.string.lbl_fortune);
		m4.put("res_id", R.drawable.ic_ft_fortune);
		m4.put("res_id_selected", R.drawable.ic_ft_fortune_selected);
		m4.put("intent", TabFortuneActivity.class);
		mTabData.put(tabIds[3], m4);

		for (int i = 0; i < tabIds.length; i++) {
			String key = tabIds[i];

			Map<String, Object> mm = mTabData.get(key);

			String tabName = getResources().getString(Integer.parseInt(mm.get("name_res_id").toString()));
			Drawable drawable = getResources().getDrawable(Integer.parseInt(mm.get("res_id").toString()));
			Intent intent = new Intent(this, (Class<?>) mm.get("intent"));

			TabHost.TabSpec tabSpec = mTabHost.newTabSpec(key).setIndicator(tabName, drawable).setContent(intent);
			mTabHost.addTab(tabSpec);
		}
		mTabHost.setCurrentTab(0);
	}

	@Override
	public void onTabChanged(String tabId) {
		LogUtil.i(tabId + " Clicked");
		TabWidget tabWidget = mTabHost.getTabWidget();

		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			Map<String, Object> mm = mTabData.get(i + "");

			View indicator = tabWidget.getChildTabViewAt(i);
			ImageView image = (ImageView) indicator.findViewById(android.R.id.icon);
			if (mTabHost.getCurrentTab() == i) {
				image.setImageDrawable(getResources().getDrawable(Integer.parseInt(mm.get("res_id_selected").toString())));
			} else {
				image.setImageDrawable(getResources().getDrawable(Integer.parseInt(mm.get("res_id").toString())));
			}
		}
	}
}
